// The Vue build version to load with the `import` command
// (runtime-only or standalone) has been set in webpack.base.conf with an alias.
import Vue from 'vue'
import App from './App'
import store from './store'
import router from './router'

import Mint from 'mint-ui';
import 'mint-ui/lib/style.css'
import VueLazyLoad from 'vue-lazyload'
Vue.use(Mint);
Vue.config.productionTip = false

if (process.env.NODE_ENV == 'development') {
    Vue.prototype.mp_id = '25845437513a9098447548e9928eacc3e5bbf64e';
    Vue.prototype.team_id = '1027052245cb5380b4174f4abec263a67f881a5f';
    Vue.prototype.user_id = '190639548a08fa32aa8641ee82758b1a6e2f18a0';
    Vue.prototype.browser_type = 1;
    Vue.prototype.wx_pay_way = 'special_sub';
    Vue.prototype.unified_pay_host = 'xueban.vstudy.net.cn';
    Vue.prototype.mp_info = {
        wx_qrcode_url: 'http://mmbiz.qpic.cn/mmbiz/13Y0Tvc8TjLnAmRGQw2Bss9ibDGDYEfEMNxO4ZicTBALYcNpwOtExEzgDwObvicCRpfGubvf950H3HTaY0kNiaVdLQ/0'
    };
    Vue.prototype.current_host = 'http://xueban.vstudy.net.cn';
} else {
    Vue.prototype.mp_id = mp_id;
    Vue.prototype.team_id = team_id;
    Vue.prototype.user_id = user_id;
    Vue.prototype.browser_type = browser_type;
    Vue.prototype.wx_pay_way = wx_pay_way;
    Vue.prototype.unified_pay_host = unified_pay_host;
    Vue.prototype.mp_info = mp_info;
    Vue.prototype.current_host = 'http://' + window.location.host;
}
Vue.prototype.img_url = 'http://image.imoocs.org/image/upload_web/';
Vue.prototype.wx_img_url = '/imageurl/handler?imgurl=';

Vue.filter('renderImg', function(value, width, height) {
        if (typeof value == 'undefined') {
            return '';
        }
        let fix_w_h = '';
        if (value.indexOf('http://') != -1) {
            if (typeof width != 'undefined' && typeof width != 'undefined') {
                fix_w_h = '?imageView2/1/w/' + width + '/h/' + height;
            }
            return value + fix_w_h;
        }

        if (typeof width != 'undefined' && typeof width != 'undefined') {
            fix_w_h = '/' + width + '/' + height;
        }
        return 'http://image.imoocs.org/image/upload_web/' + value + fix_w_h;
    })
    //图片懒加载
Vue.use(VueLazyLoad, {
    preLoad: 1.3,
    error: 'assets/images/loading.gif',
    loading: 'assets/images/loading.gif',
    attempt: 1,
    listenEvents: ['scroll', 'resize']
})
new Vue({
        el: '#app',
        router,
        store,
        template: '<App/>',
        components: { App }
    })
    // 路由控制
router.beforeEach((to, from, next) => {
    /* 路由发生变化修改页面title */
    if (to.meta.title) {
        document.title = to.meta.title;
    }
    if (to.matched.some(record => record.meta.requiresAuth)) {
        if (!Vue.prototype.user_id) {

            let Base64 = require('js-base64').Base64;
            let skip_url = Base64.encode('/mallsend#' + to.fullPath);
            window.location.href = Vue.prototype.current_host + '/mp/login?skip_url=' + skip_url;
        } else {
            next()
        }
    } else {
        next() // 确保一定要调用 next()
    }
})